using System.Collections;
using UnityEngine;

public class TypeOfEnemy : Humanoid
{
	[HideInInspector]
	protected int arrayNumForAllObjectsInfo;

	protected void SetEnemy()
	{
		StopAllCoroutines();
		at.enabled = true;
		col.enabled = true;
		COL_AttackZone.gameObject.SetActive(true);
		freezingTime = 0f;
		IKDynamicOn(false, Vector2.zero);
		COL_HitBox.enabled = true;
		attackingNow = false;
		currentOnTheFloorFlag = true;
		SCRT_OnTheFloorSensor.onTheFloorFlag = false;
		HideAttackZone();
		DestroyInHand();
		GO_Blood.SetActive(false);
		BodyTrailSwtich(0f);
		SlowMotionControl(0f);
		SetColor();
		hp = maxHP + maxHP * (float)LevelController.instance.level * 0.2f;
		AllObjectsInfo.enemy[arrayNumForAllObjectsInfo].objName = base.gameObject.name;
		AllObjectsInfo.enemy[arrayNumForAllObjectsInfo].pos = new Vector2(tf.position.x, tf.position.y + 2f);
		AllObjectsInfo.enemy[arrayNumForAllObjectsInfo].hitSize = hitSize;
		AllObjectsInfo.enemy[arrayNumForAllObjectsInfo].hp = hp;
		AllObjectsInfo.enemy[arrayNumForAllObjectsInfo].basicAttackRangeWidth = basicAttackRangeWidth;
		AllObjectsInfo.enemy[arrayNumForAllObjectsInfo].basicAttackRangeHeight = basicAttackRangeHeight;
		rb.position = AllObjectsInfo.enemy[arrayNumForAllObjectsInfo].pos;
	}

	protected IEnumerator RefreshPosition()
	{
		while (true)
		{
			AllObjectsInfo.enemy[arrayNumForAllObjectsInfo].pos = new Vector2(tf.position.x, tf.position.y + 2f);
			yield return new WaitForFixedUpdate();
		}
	}

	public void Throwing(int kind, bool justDroping = false)
	{
		SR_InHands[0].sprite = null;
		Generator.instance.SpawnObj(3, TF_InHands[0].position, new float[2]
		{
			(Player.instance.tf.position.x - tf.position.x) * 0.35f,
			(!justDroping) ? 20 : 0
		});
		Audio.instance.sfx[12].Play();
	}

	public virtual void Damaged(Vector2 attackedPos, AttackInfo attackInfo)
	{
		if (!(hp <= 0f))
		{
			SlowMotionControl(0f);
			HideAttackZone();
			attackingNow = false;
			Generator.instance.SpawnObj(9, new Vector2(TF_CenterBone.position.x, TF_CenterBone.position.y));
			hp -= attackInfo.power;
			AllObjectsInfo.enemy[arrayNumForAllObjectsInfo].hp = hp;
			Cam.instance.Shake(0.1f, 0.3f);
			Audio.instance.PlayWeaponSFX(attackInfo.kindOfWeapon);
			tf.localScale = new Vector3((!(tf.position.x > Player.instance.tf.position.x)) ? (0f - characterScale) : characterScale, characterScale, 1f);
			ComboText.instance.AddCombo();
			if (hp <= 0f)
			{
				StartCoroutine(IE_Die(attackInfo.force));
			}
			else
			{
				rb.velocity = Vector2.zero;
				at.Play(damaged_NormalList[0], -1, 0f);
			}
			GM.instance.SlowMotion(0.02f, (float)attackInfo.power * 0.0002f + 0.1f);
		}
	}

	private IEnumerator IE_Die(Vector2 forceVel)
	{
		COL_AttackZone.gameObject.SetActive(false);
		COL_HitBox.enabled = false;
		BodyTrailSwtich(0f);
		GO_Blood.SetActive(true);
		GO_Blood.transform.localScale = tf.localScale;
		yield return new WaitForSeconds(0.02f);
		at.Play("StickMan_Damaged", -1, 1f);
		EquipGetOrDrop("Enemy", false);
		AttackZoneColOn(false);
		ComboText.instance.AddScore(100);
		Audio.instance.sfx[Random.Range(5, 8)].Play();
		yield return new WaitForSeconds(0.02f);
		at.enabled = false;
		IKDynamicOn(true, forceVel);
		if (SR_InHands[0].sprite != null && SR_InHands[0].sprite.name.Contains("Grenade"))
		{
			Throwing(0, true);
		}
		StartCoroutine(IE_DeActive());
	}

	public virtual void Attack()
	{
		do
		{
			if (SCRT_WeaponAbility == null)
			{
				temp = Random.Range(0, attack_NormalList.Length);
				continue;
			}
			if (weaponAttack_NormalList.Length > 1)
			{
				temp = Random.Range(0, weaponAttack_NormalList.Length);
				continue;
			}
			temp = 0;
			currentAttackNumber = 1;
		}
		while (temp == currentAttackNumber);
		currentAttackNumber = temp;
		ShowAttackRange();
		attackingNow = true;
		if (SCRT_WeaponAbility == null)
		{
			at.Play(attack_NormalList[currentAttackNumber], -1, 0f);
		}
		else
		{
			at.Play(weaponAttack_NormalList[currentAttackNumber], -1, 0f);
		}
		SlowMotionControl(0.5f);
	}

	public void LookAtThePlayer()
	{
		if (Player.instance.hp > 0f)
		{
			tf.localScale = new Vector3((!(tf.position.x > Player.instance.tf.position.x)) ? (0f - characterScale) : characterScale, characterScale, tf.localScale.y);
		}
	}

	public void JumpToTheTarget(bool toClose = true)
	{
		if (!(Player.instance.hp <= 0f))
		{
			if (toClose)
			{
				rb.velocity = new Vector2((Player.instance.tf.position.x - AllObjectsInfo.enemy[arrayNumForAllObjectsInfo].pos.x) * Random.Range(0.7f, 1.5f), (Player.instance.tf.position.y - AllObjectsInfo.enemy[arrayNumForAllObjectsInfo].pos.y) * 1.5f + 15f * Random.Range(1f, 1.5f));
			}
			else
			{
				rb.velocity = new Vector2(0f, (Player.instance.tf.position.y - AllObjectsInfo.enemy[arrayNumForAllObjectsInfo].pos.y) * 1.5f + 13f * Random.Range(1f, 1.5f));
			}
		}
	}

	public void AppearJump()
	{
		rb.velocity = new Vector2((Player.instance.tf.position.x - AllObjectsInfo.enemy[arrayNumForAllObjectsInfo].pos.x) * Random.Range(0.5f, 1f), Random.Range(20, 25));
	}

	public virtual IEnumerator IE_Update()
	{
		while (AllObjectsInfo.enemy[arrayNumForAllObjectsInfo].hp > 0f)
		{
			yield return new WaitForFixedUpdate();
			if (kindOfMotion > 0)
			{
				kindOfMotion = 0;
			}
			else if (SCRT_OnTheFloorSensor.onTheFloorFlag && !currentOnTheFloorFlag)
			{
				currentOnTheFloorFlag = true;
				if (GetKindOfWeaponNumber() != 5 || (!at.GetCurrentAnimatorStateInfo(0).IsName("StickMan_WeaponAttackFirst") && !at.GetCurrentAnimatorStateInfo(0).IsName("StickMan_WeaponAttackSecond")))
				{
					at.Play(landing_NormalList[0], -1, 0f);
				}
				Generator.instance.SpawnObj(2, new Vector2(tf.position.x, 0f));
				Audio.instance.sfx[9].Play();
				Cam.instance.Shake(0.1f, 0.2f);
			}
			else if (!SCRT_OnTheFloorSensor.onTheFloorFlag && currentOnTheFloorFlag)
			{
				currentOnTheFloorFlag = false;
				if (!attackingNow)
				{
					at.Play(jumping_NormalList[0], -1, 0f);
				}
			}
		}
	}

	public IEnumerator IE_DrawShadow()
	{
		while (hp > 0f)
		{
			TF_Shadow.position = new Vector2(tf.position.x, 0f);
			yield return new WaitForFixedUpdate();
		}
		TF_Shadow.position = new Vector2(0f, 9999f);
	}

	public virtual IEnumerator IE_DeActive()
	{
		BodyTrailSwtich(0f);
		yield return new WaitForSeconds(3f);
		tf.position = new Vector2(0f, 9999f);
		AllObjectsInfo.enemy[arrayNumForAllObjectsInfo].pos = tf.position;
		AllObjectsInfo.SCRT_EnemyList[arrayNumForAllObjectsInfo] = null;
		base.gameObject.SetActive(false);
	}
}
